<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>工程首页</h1>
<div>
    <!--v-if 值为true则显示元素 否则不显示-->
    <div v-if="user==''"><!--user等于空字符串代表没有登陆-->
        <a href="/reg.html">注册</a>
        <a href="/login.html">登录</a>
    </div>
    <div v-if="user!=''">
        <h1>欢迎{{user.nick}}回来!</h1>
        <a href="javascript:void (0)" @click="logout()">登出</a>
    </div>
    <h1>{{info}}</h1>
</div>
<!--引入axios和vue-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
    let v = new Vue({
        el:"body>div",
        data:{
            info:"",
            user:""
        },
        created:function () {
            //发出获取当前登录用户对象的请求
            axios.get("/currentUser").then(function (response) {
                v.user = response.data;
                console.log("user = "+v.user);
                v.info = response.data==""?"未登录":"已登录";
            })
        },
        methods:{
            logout:function () {
                if(confirm("您确定要退出登录吗?")){
                    axios.get("/logout").then(function () {
                        location.reload();
                    })
                }

            }
        }
    })
</script>
</body>
</html>